package com.morningstar.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.morningstar.system.dao.entity.SysUser;
import com.morningstar.system.dao.entity.SysUserPost;
import com.morningstar.system.dao.mapper.SysUserPostMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SysUserPostService extends ServiceImpl<SysUserPostMapper, SysUserPost> {

    public void addUserPost(SysUser sysUser) {
        this.remove(new LambdaQueryWrapper<SysUserPost>().eq(SysUserPost::getUserId, sysUser.getUserId()));
        List<SysUserPost> userPostList = new ArrayList<>();
        SysUserPost userPost;
        for (Long postId : sysUser.getPostIds()) {
            userPost = new SysUserPost();
            userPost.setUserId(sysUser.getUserId());
            userPost.setPostId(postId);
            userPostList.add(userPost);
        }
        this.saveBatch(userPostList);
    }
}
